<template>
  <div>
    <van-nav-bar
      title="城市列表"
      left-arrow
      @click-left="$router.go(-1)"
      :fixed="true"
      :z-index="100"
    />
    <div class="city">
      <van-index-bar :index-list="indexList" sticky>
        <div v-for="(item, key) in allList" :key="key">
          <van-index-anchor :index="key">
            <span v-if="key === '#'" style="color: #777; font-size: 10px"
              >当前城市</span
            >
            <span v-else-if="key === '热'" style="color: #777; font-size: 10px"
              >热门城市</span
            >
            <span v-else style="color: #777; font-size: 20px">{{ key }}</span>
          </van-index-anchor>
          <van-cell
            @click="onClick(item1)"
            :title="item1.label"
            v-for="(item1, index) in item"
            :key="index"
          />
        </div>
      </van-index-bar>
    </div>
  </div>
</template>

<script>
import { setToken, getToken } from '@/utils/storage'
import { hotCity, allCity } from '@/api/home'
export default {
  async created() {
    const {
      data: { body }
    } = await hotCity()

    const {
      data: { body: res }
    } = await allCity()

    const allList = {
      '#': [
        JSON.parse(getToken()) || {
          label: '上海',
          value: 'AREA|dbf46d32-7e76-1196',
          pinyin: 'shanghai',
          short: 'sh'
        }
      ],
      热: [],
      A: [],
      B: [],
      C: [],
      D: [],
      E: [],
      F: [],
      G: [],
      H: [],
      I: [],
      J: [],
      K: [],
      L: [],
      M: [],
      N: [],
      O: [],
      P: [],
      Q: [],
      R: [],
      S: [],
      T: [],
      U: [],
      V: [],
      W: [],
      X: [],
      Y: [],
      Z: []
    }
    allList['热'] = body
    res.forEach((item) => {
      const key = item.short[0].toUpperCase()
      allList[key].push(item)
    })
    this.allList = allList
  },
  data() {
    return {
      hotCity: [],
      indexList: [
        '#',
        '热',
        'A',
        'B',
        'C',
        'D',
        'E',
        'F',
        'G',
        'H',
        'I',
        'J',
        'K',
        'L',
        'M',
        'N',
        'O',
        'P',
        'Q',
        'R',
        'S',
        'T',
        'U',
        'V',
        'W',
        'X',
        'Y',
        'Z'
      ],
      allList: []
    }
  },
  methods: {
    onClick(value) {
      setToken(value)
      this.$router.push('/home')
    }
  }
}
</script>

<style scoped lang="less">
.van-nav-bar {
  background-color: #21b97a;
}
::v-deep .van-index-bar__index {
  margin: 3px 0;
}
.city {
  position: fixed;
  top: 48px;
  bottom: 0;
  left: 0;
  right: 0;
  overflow: auto;
}
</style>
